package file;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class FileDemo {
    public static void main(String[] args) throws IOException {
        File file = new File("demo.txt");
        if (file.exists()) {
            System.out.println("该文件已存在。");
        } else {
            System.out.println("文件是否创建：" + file.createNewFile());
        }
        File dir = new File("./a/b/c/d");
        if (dir.exists()) {
            System.out.println("该目录已存在。");
        } else {
            System.out.println("该目录是否创建：" + dir.mkdirs());
            System.out.println("file父目录："+file.getParentFile());
        }
        if (file.exists()){
            System.out.println("文件已删除："+file.delete());
        }else {
            System.out.println("文件不存在");
        }
        if (dir.exists()){
            System.out.println("目录已删除："+dir.delete());
        }else {
            System.out.println("目录不存在。");
        }
        File file1 = new File("src");
        System.out.println("src目录下组成：" + Arrays.toString(file1.listFiles()));
        System.out.println("src父目录："+file1.getParentFile());
        File file2 = new File("demo/HelloWorld");
        if (file2.getParentFile().exists()){
            System.out.println("file2父目录已存在");
        }else {
            System.out.println("父目录是否创建："+file2.getParentFile().mkdirs());
        }
        if (file2.exists()){
            System.out.println("file2已存在。");
        }else {
            System.out.println("file2已创建："+file2.createNewFile());
        }
        File file3 = new File("demo","f.txt");
        boolean newFile = file3.createNewFile();
        System.out.println(file2.getName());
        File file4 = new File(".");
        File[] files = file4.listFiles(f->f.getName().startsWith("."));
        assert files != null;
        for (File f:files){
            System.out.print(f+"\t");
        }
        System.out.println();
        System.out.println(file4.length());
    }
}
